package com.julis.activity.record;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.excel.ExcelUtils;
import com.julis.bean.ObuTrade_Info;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.julis.R;
import com.julis.activity.record.adapter.MyRVAdapter;
import com.julis.bean.Obu_Info;
import com.julis.db.Z_ObuInfoDao;

/**
 * 检测记录列表
 */

public class Record_List_Activity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.recyclerView)
    RecyclerView mRvMain;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    MyRVAdapter mAdapter;
    Z_ObuInfoDao z_obuInfoDao;
    List<Obu_Info> obu_infos;

    @OnClick({R.id.img_back, R.id.bt_daochu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_daochu:
                out_excel();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_list);
        ButterKnife.bind(this);
        z_obuInfoDao = new Z_ObuInfoDao(this);
        obu_infos = z_obuInfoDao.queryForAll();
// 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvMain.setLayoutManager(linearLayoutManager);
        mRvMain.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this).build());
        mAdapter = new MyRVAdapter(this);
        mRvMain.setAdapter(mAdapter);
        mAdapter.addItem(obu_infos);
        mAdapter.setOnItemClickListener(new MyRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(UIUtil.getContext(), "click" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
//                Toast.makeText(UIUtil.getContext(), "long click" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    private String fileName;
    private ArrayList<ArrayList<String>> recordList;

    File file;
    private static String[] title = {"编号", "vin号", "obu编号", "Vin录入",
            "obu录入", "OBU自检", "obu检测", "蓝牙检测", "vin写入"};

    /**
     * 导出excel
     *
     * @param
     */
    public void out_excel() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH_mm_ss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        file = new File(getSDPath() + "/Record");
        makeDir(file);
        updata(this, file);
        ExcelUtils.initExcel(file.toString() + "/流水线检测" + str + ".xls", title, this);
        fileName = getSDPath() + "/Record/流水线检测" + str + ".xls";
        ExcelUtils.writeObjListToExcel(getRecordData(), fileName, this);
    }

    public static void updata(Context context, File file) {
        MediaScannerConnection.scanFile(context,
                new String[]{file.getAbsolutePath()}, null, null);
    }

    /**
     * 将数据集合 转化成ArrayList<ArrayList<String>>
     *
     * @return
     */
    private ArrayList<ArrayList<String>> getRecordData() {
        recordList = new ArrayList<>();
        for (int i = 0; i < obu_infos.size(); i++) {
            Obu_Info obuTradeInfo = obu_infos.get(i);
            ArrayList<String> beanList = new ArrayList<String>();
            beanList.add(""+(i+1) );
            beanList.add(obuTradeInfo.getVincode());
            beanList.add(obuTradeInfo.getObu_code());
            beanList.add(obuTradeInfo.getVin_input_success());
            beanList.add(obuTradeInfo.getObu_input_success());
            beanList.add(obuTradeInfo.getKl15_success());
            beanList.add(obuTradeInfo.getObu_check_success());
            beanList.add(obuTradeInfo.getBle_check_success());
            beanList.add(obuTradeInfo.getVin_write_success());

            recordList.add(beanList);
        }
        return recordList;
    }

    private String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
        }
        String dir = sdDir.toString();
        return dir;
    }

    public void makeDir(File dir) {
        if (!dir.getParentFile().exists()) {
            makeDir(dir.getParentFile());
        }
        dir.mkdir();
    }
}
